﻿using ConfExpert.Forms.Base;
using DevExpress.LookAndFeel;
using DevExpress.Utils.Drawing;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Layout;
using DevTeam;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class RBSirali : XForm
    {
        public RBSirali()
        {
            InitializeComponent();
        }

        //string[] cats = @"1. Düz;2. Küçük kareli;3. Büyük Kareli;4. Kalın Çizgili;5. İnce Çizgi;6. Baskılı".Split(";".ToCharArray());
        private void RBSirali_Load(object sender, EventArgs e)
        {
            
            spinEdit1.EditValue = 0;
            spinEdit1.EditValueChanged += spinEdit1_EditValueChanged;
            //spinEdit1.Properties.AutoHeight = false;
            //spinEdit1.Height = 30;
            //((RepositoryItemSpinEdit)spinEdit1.Edit).Properties.Buttons[0].Width = 15;

            ((RepositoryItemComboBox)comboBoxEdit1.Edit).TextEditStyle = TextEditStyles.DisableTextEditor;
            ((RepositoryItemComboBox)comboBoxEdit1.Edit).SelectedIndexChanged += comboBoxEdit1_SelectedIndexChanged;
            //comboBoxEdit1.EditValue = "Düz";

            Notify("Kumaş resimlerine Ctrl+Click: büyük resmi getirir.");
            InitLV();


            btnScreenshot.ItemClick += btnScreenshot_ItemClick;
        }

        void btnScreenshot_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TakeScreenShot();
        }

        void spinEdit1_EditValueChanged(object sender, EventArgs e)
        {
            spinEdit1.Enabled = false;
            gcRenkler.DataSource = RenewData();
            spinEdit1.Enabled = true;
        }

        void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((int)spinEdit1.EditValue < 1)
                gcRenkler.DataSource = RenewData();

            spinEdit1.EditValue = 0;
        }

        LayoutViewField lvfPhoto;
        int sz = 150;
        Color color1 = Color.Red;
        Color color2 = Color.Transparent;

        List<int> selection = new List<int>();
        private void lvRenkler_CustomDrawCardFieldValue(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            LayoutView view = sender as LayoutView;
            if (e.Column.FieldName.Contains("F"))
            {
                if (selection.Contains(intParse(view.GetRowCellValue(e.RowHandle, "Id"))))
                {
                    Rectangle rect = new Rectangle(e.Bounds.X , e.Bounds.Y - 1, e.Bounds.Width, e.Bounds.Height + 1);
                    e.Graphics.DrawRectangle(new Pen(color1, 5), rect);
                }
                else
                {
                    Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y - 1, e.Bounds.Width, e.Bounds.Height);
                    e.Graphics.DrawRectangle(new Pen(color2, 5), rect);
                }
            }

        }


        int dosyano = 0;
        public void TakeScreenShot()
        {
            MemoryStream memoryStream = new System.IO.MemoryStream();
            Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
            Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot);
            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

            byte[] imageBytes = null;
            using (MemoryStream ms = new MemoryStream())
            {
                bmpScreenshot.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                imageBytes = ms.ToArray();
            }
            File.WriteAllBytes(@"C:\Users\Administrator\Desktop\screenshots\" + (++dosyano).ToString() + ".png", imageBytes); 
        }

        void InitLV()
        {

            //lvRenkler.TemplateCard.MaxSize = new Size(x, ilkAyrim);
            lvRenkler.Click -= new EventHandler(lvRenkler_Click);
            lvRenkler.CustomDrawCardFieldValue -= new DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventHandler(lvRenkler_CustomDrawCardFieldValue);
            lvRenkler.Columns.Clear();

            lvRenkler.GridControl.DataSource = RenewData();
            //lvRenkler.Columns[1].DisplayFormat = "{0:StokKodu:n}";

            lvRenkler.PopulateColumns();
            lvRenkler.OptionsBehavior.Editable = false;


            lvRenkler.Click += new EventHandler(lvRenkler_Click);
            lvRenkler.CustomDrawCardFieldValue += new DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventHandler(lvRenkler_CustomDrawCardFieldValue);
            lvRenkler.Columns["F"].ColumnEdit = new RepositoryItemPictureEdit();
            lvfPhoto = lvRenkler.Columns["F"].LayoutViewField;
            lvfPhoto.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
            lvfPhoto.MinSize = lvfPhoto.MaxSize = new Size(sz, sz);  //140x140 idi

            lvRenkler.CardMinSize = new Size(sz, sz);
            lvRenkler.OptionsMultiRecordMode.MultiRowScrollBarOrientation = DevExpress.XtraGrid.Views.Layout.ScrollBarOrientation.Vertical;
            lvRenkler.OptionsView.ViewMode = DevExpress.XtraGrid.Views.Layout.LayoutViewMode.MultiRow;
            lvRenkler.OptionsView.ShowCardCaption = false;
            lvRenkler.OptionsView.ShowCardExpandButton = false;
            lvRenkler.OptionsBehavior.UseTabKey = true;
            lvRenkler.OptionsBehavior.KeepFocusedRowOnUpdate = true;

            lvRenkler.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;

            foreach (LayoutViewField lvf in lvRenkler.TemplateCard.Items)
            {
                lvf.Padding = new DevExpress.XtraLayout.Utils.Padding(4, 4, 0, 0);
                lvf.EditorPreferredWidth = sz;
            }
            layoutViewCard1.Padding = new DevExpress.XtraLayout.Utils.Padding(2, 2, 0, 0);

            lvRenkler.Columns["Id"].Caption = "ID";

            foreach (LayoutViewColumn lvc in lvRenkler.Columns)
            {
                lvc.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
                lvc.OptionsFilter.AllowFilter = false;
            }

        }

        private DataTable RenewData()
        {
            int sel = 0;

            if (comboBoxEdit1.EditValue != null)
                switch (comboBoxEdit1.EditValue.ToString())
                {
                    case "Düz": sel = 1; break;
                    case "Küçük Kareli": sel = 2; break;
                    case "Büyük Kareli": sel = 3; break;
                    case "Kalın Çizgili": sel = 4; break;
                    case "İnce Çizgili": sel = 5; break;
                    case "Baskılı": sel = 6; break;

                }

            return Session.DataManager.ExecuteDataTable("GetRBSirali", intParse(spinEdit1.EditValue), sel);
        }


        void lvRenkler_Click(object sender, EventArgs e)
        {
                int id = intParse(lvRenkler.GetFocusedDataRow()["Id"].ToString());

                if (Control.ModifierKeys == Keys.Control)
                    Goster(id);
                else
                    if (selection.Contains(id))
                        selection.Remove(id);
                    else
                        selection.Add(id);
        }
    }








    class MySpinEdit : SpinEdit
    {
        public MySpinEdit()
        {

        }

        static MySpinEdit()
        {
            RepositoryItemMySpinEdit.Register();
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemMySpinEdit Properties
        {
            get { return base.Properties as RepositoryItemMySpinEdit; }
        }

        public override string EditorTypeName
        {
            get
            {
                return RepositoryItemMySpinEdit.EditorName;
            }
        }

    }


    [UserRepositoryItem("Register")]
    public class RepositoryItemMySpinEdit : RepositoryItemSpinEdit
    {
        internal const string EditorName = "MySpinEdit";

        static RepositoryItemMySpinEdit()
        {
            Register();
        }

        public RepositoryItemMySpinEdit()
        {

        }
        public static void Register()
        {
            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(EditorName, typeof(MySpinEdit),
                typeof(RepositoryItemMySpinEdit), typeof(MyBaseSpinEditViewInfo),
                new DevExpress.XtraEditors.Drawing.ButtonEditPainter(), true, null, typeof(DevExpress.Accessibility.ButtonEditAccessible)));
        }
        public override string EditorTypeName
        {
            get { return EditorName; }
        }

    }


    class MyBaseSpinEditViewInfo : BaseSpinEditViewInfo
    {
        public MyBaseSpinEditViewInfo(RepositoryItem item)
            : base(item)
        {

        }

        protected override EditorButtonPainter CreateButtonPainter()
        {
            if (this.Item.ButtonsStyle == BorderStyles.Default)
            {
                if (this.LookAndFeel == null)
                {
                    this.LookAndFeel = UserLookAndFeel.Default;
                }
                if (this.LookAndFeel.ActiveStyle == ActiveLookAndFeelStyle.WindowsXP)
                {
                    return new WindowsXPEditorButtonPainter();
                }
                if (this.LookAndFeel.ActiveStyle == ActiveLookAndFeelStyle.Skin)
                {
                    return new SkinEditorButtonPainter(this.LookAndFeel.ActiveLookAndFeel);
                }
                if (this.LookAndFeel.ActiveStyle != ActiveLookAndFeelStyle.Office2003)
                {
                    MyEditorButtonPainter mebp = new MyEditorButtonPainter(this.LookAndFeel.Painter.Button);
                    mebp.EditorHeight = OwnerEdit.Height;
                    return mebp;
                }
                //this.Item.ButtonsStyle = BorderStyles.Office2003;
            }
            if (this.Item.ButtonsStyle == BorderStyles.Office2003)
            {
                this.Item.ButtonsStyle = BorderStyles.Default;
            }

            Painters[(int)this.Item.ButtonsStyle].EditorHeight = OwnerEdit.Height;
            return Painters[(int)this.Item.ButtonsStyle];

        }
        private static MyEditorButtonPainter[] painters;
        private static MyEditorButtonPainter[] Painters
        {
            get
            {
                if (painters == null)
                {
                    CreatePainters();
                }
                return painters;
            }
        }

        private static void CreatePainters()
        {
            painters = new MyEditorButtonPainter[Enum.GetValues(typeof(BorderStyles)).Length];
            painters[0] = new MyEditorButtonPainter(new FlatButtonObjectPainter());
            painters[1] = new MyEditorButtonPainter(new SimpleButtonObjectPainter());
            painters[2] = new MyEditorButtonPainter(new FlatButtonObjectPainter());
            painters[3] = new MyEditorButtonPainter(new HotFlatButtonObjectPainter());
            painters[4] = new MyEditorButtonPainter(new UltraFlatButtonObjectPainter());
            painters[5] = new MyEditorButtonPainter(new Style3DButtonObjectPainter());
            painters[6] = new MyEditorButtonPainter(new Office2003ButtonObjectPainter());
            painters[7] = new MyEditorButtonPainter(new Office2003ButtonInBarsObjectPainter());
        }

    }

    class MyEditorButtonPainter : EditorButtonPainter
    {
        private int editorHeight;

        public int EditorHeight
        {
            get { return editorHeight; }
            set { editorHeight = value; }
        }

        public MyEditorButtonPainter(ObjectPainter buttonPainter)
            : base(buttonPainter)
        {

        }

        protected override void DrawArrow(EditorButtonObjectInfoArgs e, Rectangle rect)
        {
            int d = editorHeight / 4;
            Brush foreBrush = this.GetForeBrush(e);

            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.SpinUp)
            {
                e.Cache.Paint.FillPolygon(e.Graphics, foreBrush, new Point[] { new Point(rect.Left - d, rect.Bottom + d / 2), new Point(rect.Right + d, rect.Bottom + d / 2), new Point(rect.Left + rect.Width / 2, rect.Top - d / 2) });
            }
            else if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.SpinDown)
            {
                e.Cache.Paint.FillPolygon(e.Graphics, foreBrush, new Point[] { new Point(rect.Left - d, rect.Top - d / 2), new Point(rect.Right + d, rect.Top - d / 2), new Point(rect.Left + rect.Width / 2, rect.Bottom + d / 2) });
            }
            else
                base.DrawArrow(e, rect);
        }
    }

}
